#!/usr/bin/perl
# 2013/11/19 change file name

use strict;
use warnings;
use IO::File;
use Getopt::Std;
use Carp qw(croak);
$| = 0;


if($ARGV[0] eq '--help'){
	die "Usage: perl $0 -d [directory or data]\n";
}


my %opt;
getopts("d:", \%opt);
my $DIR = $opt{d};

#---------------------------------------
# original file names
# E05_[欲しい名前]_M13R_007.[拡張子]
#---------------------------------------

opendir DIR, $DIR or die "cannot open file: $!";
while(my $file = readdir DIR){
	if($file =~ /^\./){
		next;
	}
	my $oldName = $DIR . "/" . $file;
	if($file =~ m/\w\d\d_(.+)_M13[^_]+_\d\d\d(\.\w+)/){
		my $newName = $DIR . "/" . $1 . $2;
		rename $oldName, $newName or die "$oldName cannot change to $newName: $!";
	}
}